//
//  NSObject+category.m
//  OneStore
//
//  Created by Seven on 14-3-23.
//  Copyright (c) 2014年 OneStore. All rights reserved.
//

#import "NSObject+category.h"


@implementation NSObject (category)

static char associatedObjectNamesKey;

- (void)setAssociatedObjectNames:(NSMutableArray *)associatedObjectNames
{
    objc_setAssociatedObject(self, &associatedObjectNamesKey, associatedObjectNames,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableArray *)associatedObjectNames
{
    NSMutableArray *array = objc_getAssociatedObject(self, &associatedObjectNamesKey);
    if (!array) {
        array = [NSMutableArray array];
        [self setAssociatedObjectNames:array];
    }
    return array;
}

- (void)objc_setAssociatedObject:(NSString *)propertyName value:(id)value policy:(objc_AssociationPolicy)policy
{
    id impBlockForIMP = (__bridge id)(__bridge void *)(propertyName);
    
    
    objc_setAssociatedObject(self, (__bridge void *)(impBlockForIMP), value, policy);
    [self.associatedObjectNames addObject:propertyName];
}

- (id)objc_getAssociatedObject:(NSString *)propertyName
{
    
    id impBlockForIMP = (__bridge id)(__bridge void *)(propertyName);
    
    return objc_getAssociatedObject(self, (__bridge void *)(impBlockForIMP));
}

- (void)objc_removeAssociatedObjects
{
    [self.associatedObjectNames removeAllObjects];
    objc_removeAssociatedObjects(self);
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    NSLog(@"setValue %@ forUndefinedKey %@",value,key);
}

- (void)setNilValueForKey:(NSString *)key
{
    NSLog(@"setNilValue forKey %@",key);
}

@end


@implementation NSUserDefaults(add)

+ (id)getValueForKey:(NSString *)key{
    return [[NSUserDefaults standardUserDefaults] valueForKey:key];
}
+ (void)saveValue:(id)value forKey:(NSString *)key{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    if (value == nil) {
        [ud removeObjectForKey:key];
    }else {
        [ud setValue:value forKey:key];
    }
    [ud synchronize];
}
@end
